package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author fxw
 * @create 2021/8/7 23:00s
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    //注入orderService
    @Reference
    private OrderService orderService;


    /**
     * @author fxw
     * @param map  页面接收的分页查询条件
     * @return
     * @throws Exception
     */
    //分页查找预约全部信息
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody Map map) throws Exception {
        //调用service层,查询数据
        return orderService.findPage(map);
    }

    /**
     * @author fxw
     * @param map  页面post发送的数据
     * @param setmealId  页面get发送的数据
     * @return
     */
    @RequestMapping("/addOrder_fxw")
    public Result addOrder_fxw(@RequestBody Map<String,Object> map,String setmealId) throws Exception {
        //调用OrderService,添加预约信息
        return orderService.addOrder_fxw(map,setmealId);
    }

    @RequestMapping("/editOrder")
    public Result editOrder(@RequestBody Map<String,Object> map, String setmealId , String orderId){
        try{
            return orderService.editOrder(map,setmealId,orderId);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_ORDERINFO_FAIL);
        }
    }
    @RequestMapping("/editOrderStatus")
    public Result editOrderStatus(@RequestBody Map<String,Object> map){
        try {
            String orderStatus = (String) map.get("orderStatus");
            Integer orderId = (Integer) map.get("orderId");
            orderService.editOrderStatus(orderStatus, orderId);
            return new Result(true,MessageConstant.EDIT_ORDERSTATUS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ORDERSTATUS_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('DELETE_ORDER_ONE')")
    @RequestMapping("/delete")
    public Result delete(String id){
        return new Result(false,"权限不足");
    }

}
